/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.simulation.view.shapes;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.StaticBody;
import net.phys2d.raw.shapes.Box;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.ShapeRenderer;
import org.newdawn.slick.geom.TexCoordGenerator;

/**
 * Ein Boden (statisch)
 */
public class Ground extends CustomShape {

	private Image grass;
	private float width;
	private float rotation;
	private float height;
	private TexCoordGenerator texPaint;
	private Rectangle texRect;
	private Polygon shape;

	/**
	 * Konstruktor
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param rotation
	 * @param gc
	 */
	public Ground(float x, float y, float width, float height, float rotation, GameContainer gc) {
		super(x, y, gc);
		this.rotation = rotation;
		this.width = width;
		this.height = height;

		initPhysics();
	}

	/**
	 * Konstruktor
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param gc
	 */
	public Ground(float x, float y, float width, float height, GameContainer gc) {
		this(x, y, width, height, 0, gc);
	}

	@Override
	public void initPhysics() {
		Box box = new Box(width, height);
		StaticBody ground = new StaticBody("Ground", box);
		ground.setPosition(x, y);
		ground.setFriction(1.0f);
		ground.setRotation((float) Math.toRadians(rotation));
		entities.add(ground);
		try {
			grass = new Image("icons_sim/edge_grass_small.png");
		} catch (SlickException e) {
			e.printStackTrace();
		}

		Vector2f[] pts = box.getPoints(ground.getPosition(), ground.getRotation());

		Vector2f v1 = pts[0];
		Vector2f v2 = pts[1];
		Vector2f v3 = pts[2];
		Vector2f v4 = pts[3];

		shape = new org.newdawn.slick.geom.Polygon();
		shape.addPoint(v1.x, v1.y - 1);
		shape.addPoint(v2.x, v2.y - 1);
		shape.addPoint(v3.x, v3.y + 1);
		shape.addPoint(v4.x, v4.y + 1);

		texRect = new Rectangle(x, y, 32, 32);
		texPaint = new TexCoordGenerator() {
			public org.newdawn.slick.geom.Vector2f getCoordFor(float x, float y) {
				float tx = (texRect.getX() - x) / texRect.getWidth();
				float ty = (texRect.getY() - y) / texRect.getHeight();
				return new org.newdawn.slick.geom.Vector2f(tx, ty);
			}
		};
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.white);
		ShapeRenderer.texture(shape, grass, texPaint);
	}

	@Override
	public void update(int delta) {
		// nothing
	}
}